package com.jeecg.demo.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecgframework.jwt.util.ResponseMessage;
import org.jeecgframework.jwt.util.Result;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Api(value = "VPostorder", description = "交易", tags = "VPostorder")
@Controller
@RequestMapping("/vPostorder")
public class HotOrderList {
	@Autowired
	private SystemService systemService;

	/**
	 * 热销产品
	 */
	@RequestMapping(value = "/hotOrderList", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "热销产品", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> hotOrderList() {
		String sql = "SELECT A.* FROM (SELECT T.POID,T.POCODE,T.COMMODITYID,T.POTRADERNAME,T.COMMODITYNAME,T.COMMODITYQUALITY,T.FULLNAME||' '||T.DELIVERPLACE AS DELIVERPLACE, T.DELIVERSTARTDATE,"
       +" T.DELIVERENDDATE,T.REMAININGQTY,T.MINTRADEQTY,T.DIRECTION,T.DIRECTIONNAME,T.PRICETYPENAME,T.POPRICE,T.ACCNUM,T.NOTE,T.VALIDSTARTDATE,T.VALIDENDDATE,"
       +" T.TRANSPORTTYPE,T.TRANSPORTTYPENAME,T.VARIETYNAME,ROW_NUMBER() OVER(ORDER BY POID DESC) AS RN"
        +" FROM V_POSTORDER T left join  LISTOBJS a on a.objid=t.poid  where  a.objtype=2 and a.berthid=8 order by a.listorder asc) A WHERE RN < 4 ";
		List<Map<String, Object>> listMap = systemService.findForJdbc(sql);
		return Result.success(listMap);
	}

	/**
	 * 最新发布的挂单列表
	 */
	@RequestMapping(value = "/postOrderList", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "最新挂单列表", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> newPostorderList() {
		String sql = "SELECT A.* FROM (SELECT  T.POID,  T.POCODE,  T.COMMODITYID, T.POTRADERNAME,    T.COMMODITYNAME,  T.COMMODITYQUALITY,  T.FULLNAME || ' ' || T.DELIVERPLACE AS DELIVERPLACE,"
                      +"T.DELIVERSTARTDATE,  T.DELIVERENDDATE,   T.REMAININGQTY,   T.MINTRADEQTY,  T.DIRECTION,   T.DIRECTIONNAME,  T.PRICETYPENAME,   T.POPRICE,  T.ACCNUM,  T.NOTE,   T.VALIDSTARTDATE, " 
                      +"T.VALIDENDDATE, T.TRANSPORTTYPE,  T.TRANSPORTTYPENAME,  T.VARIETYNAME,  T.FIELD1 AS CHAND,   ROW_NUMBER() OVER(ORDER BY POID DESC) AS RN"
                   +" FROM v_ecp_postorder T where t.POSTATUS in (3) and t.trademodeid = 1  ) A  WHERE RN < 6  ORDER BY A.POID DESC";
		List<Map<String, Object>> listMap = systemService.findForJdbc(sql);
		return Result.success(listMap);
	}

	 /**
     * 成交信息接口
     */
    @RequestMapping(value = "/traderOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "成交信息", produces = "application/json")
    public ResponseMessage<List<Map<String, Object>>> traderOrderList() {
		String sql = " SELECT\n" +
				"\tA.* \n" +
				"FROM\n" +
				"\t(\n" +
				"\tSELECT\n" +
				"\t\tTOID,\n" +
				"\t\tPOID,\n" +
				"\t\tAOID,\n" +
				"\t\tTRADEMODEID,\n" +
				"\t\tPOTRADERID,\n" +
				"\t\tPOTRADERNAME,\n" +
				"\t\tPOUSERID,\n" +
				"\t\tAOTRADERID,\n" +
				"\t\tAOTRADERNAME,\n" +
				"\t\tAOUSERNAME,\n" +
				"\t\tCOMMODITYNAME,\n" +
				"\t\tCOMMODITYQUALITY,\n" +
				"\t\tTOPRICE,\n" +
				"\t\tTOQUALITY,\n" +
				"\t\tTOTIME,\n" +
				"\t\tPOCODE,\n" +
				"\t\tpoquantity,\n" +
				"\t\ttostatus,\n" +
				"\t\tROW_NUMBER ( ) OVER ( ORDER BY TOID DESC ) AS RN ,  direction\n" +
				"\tFROM\n" +
				"\t\tv_ecp_tradeorder \n" +
				"\t) A \n" +
				"WHERE\n" +
				"\ta.tostatus = '1' \n" +
				"AND\n" +
				"\ta.POTRADERNAME is NOT NULL\n" +
				"ORDER BY\n" +
				"\tA.TOID DESC ";
		List<Map<String, Object>> listMap = systemService.findForJdbc(sql);
        return Result.success(listMap);
    }

	/**
	 * 获取实时交易数据信息
	 */
	@RequestMapping(value = "/transactionData", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "实时交易数据信息", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> transactionData() {
		String sql = " SELECT\n" +
				"\thet.QIANYSJ AS \"date\",\n" +
				"\thet.CHUMDWMC AS \"seller\",\n" +
				"\thet.HETBH AS \"number\",\n" +
				"\thetkz.SHUL AS \"amount\",\n" +
				"\thet.PINP AS \"variety\",\n" +
				"\thetkz.JIAG AS \"price\",\n" +
				"\thet.MAISDWMC AS \"buyer\" \n" +
				"FROM\n" +
				"\tMT_GL_HET het\n" +
				"\tLEFT JOIN MT_GL_HETKZ hetkz ON het.hetid = hetkz.hetid \n" +
				"WHERE\n" +
				"\thet.ZHUANGT = 4 ";
		List<Map<String, Object>> listMap = systemService.findForJdbc(sql);
		return Result.success(listMap);
	}

	/**
	 * 获取成交总量信息
	 */
	@RequestMapping(value = "/sexualIntercourse", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "成交总量信息", produces = "application/json")
	public ResponseMessage<List<Map<String, Object>>> sexualIntercourse() {
		String sql = "SELECT\n" +
				"\tSUM(tr.TOQUALITY) as sum\n" +
				"FROM\n" +
				"\tTRADEORDER tr\n" +
				"WHERE\n" +
				"\ttr.TOSTATUS = 1";
		List<Map<String, Object>> listMap = systemService.findForJdbc(sql);
		return Result.success(listMap);
	}
}
